package com.qwz.example;

/**
 * @author Qiuwuzhi
 * @version V1.0
 * @Description: 模拟运行流程A，只是一个示意，代指某个具体流程
 * @Package com.qwz.example.FlowAMock
 * @date 2019/12/26 9:58
 */
public class FlowAMock {
    /**
     * 流程名称，不需要外部存储的状态数据
     */
    private String flowName;
    /**
     * 指代某个中间结果，需要外部存储的状态数据
     */
    private int tempResult;

    /**
     * 指代某个中间结果，需要外部存储的状态数据
     */
    private String tempState;

    public FlowAMock(String flowName) {
        this.flowName = flowName;
    }

    /**
     * 运行流程的第一个阶段
     */
    public void runPhaseOne() {
        tempResult = 3;
        tempState = "PhaseOne";
    }

    public void schema1() {
        this.tempState += ",Schema1";

        System.out.println(this.tempState + "：now run" + this.tempResult);
        this.tempResult += 11;
    }

    public void schema2() {
        this.tempState += ",Schema2";
        System.out.println(this.tempState + "：now run" + tempResult);
        this.tempResult += 22;
    }

    /**
     * 创建保存原发器对象状态的备忘录对象
     * @return
     */
    public FlowAMockMemento createMemento() {
        return new MementoImpl(this.tempResult, this.tempState);
    }

    /**
     * 重新设置原发器对象的状态，让其回到备忘录对象记录的状态
     * @param memento
     */
    public void setMemento(FlowAMockMemento memento) {
        MementoImpl mementoImpl = (MementoImpl) memento;
        this.tempState = mementoImpl.getTempState();
        this.tempResult = mementoImpl.getTempResult();
    }

    private class MementoImpl extends FlowAMockMemento {
        /**
         * 保存某个中间结果
         */
        private int tempResult;
        /**
         * 保存某个中间结果
         */
        private String tempState;

        public MementoImpl(int tempResult, String tempState) {
            this.tempResult = tempResult;
            this.tempState = tempState;
        }

        public int getTempResult() {
            return tempResult;
        }

        public String getTempState() {
            return tempState;
        }
    }
}
